package named;

class MyRunnable implements Runnable{

    @Override
    public void run() {
        long result=0;
        for(int i=1;i<=100;i++){
            result += i;
            System.out.println(Thread.currentThread().getName() + ",i=" + i);//获取当前线程的名字
            try {
                Thread.sleep(50); //当前线程休眠50毫秒

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("result=" + result);
    }
}


public class ThreadNamedDemo {

    public static void main(String[] args) {
        Thread th = new Thread(new MyRunnable());
        th.setName("我的线程1");//给线程命名
        th.start();//启动线程

        for(int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName()+",i=" + i);
            try {
                Thread.sleep(200);
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
    }
}
